package com.huike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.builder.UserBuilder;
import com.huike.user.dtos.LoginDTO;
import com.huike.user.pojos.User;
import com.huike.mapper.UserMapper;
import com.huike.result.Result;
import com.huike.service.UserService;
import com.huike.utils.CreateExcelUtils;
import com.huike.utils.POIUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final static Integer createRow=8;
    private final static Integer createColumn=8;

    @Autowired
    private CaptchaServiceImpl captchaService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void listUser() {
        List<User> list = list();
        System.out.println(list);
    }

    @Override
    public Result login(LoginDTO dto) {
        if(dto==null){
            return Result.error("无效参数");
        }
        if(dto.getUsername()==null||dto.getPassword()==null){
            return Result.error("账号/密码不能为空");
        }
        User user = getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, dto.getUsername()));
        if(user==null){
            return Result.error("账号不存在");
        }
        String clientPassword=dto.getPassword();
        if(!clientPassword.equals(user.getPassword())){
            return Result.error("密码错误");
        }
        String redisPass = redisTemplate.opsForValue().get("kaptcha:");
        if (clientPassword!=null&&redisPass!=null&&redisPass.equals(dto.getVeriCode())) {
            redisTemplate.delete("kaptcha:");
            return Result.success("登录成功");
        }else {
            return Result.error("验证码错误");
        }

    }

    /**
     * 下载模版
     * @return
     */
    @Override
    public Result download() {
        CreateExcelUtils.createExcel(createRow,createColumn);
        return Result.success("下载成功");
    }

    /**
     * 批量导入用户
     * @param file
     * @return
     */
    @Override
    public Result upload(MultipartFile file) {
        //参数校验
        if (file==null){
            return Result.error("参数错误");
        }
        List<String[]> list=null;
        try {
            list = POIUtils.readExcel(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (list==null){
            return Result.error("表格为空");
        }
        List<User> userList=new ArrayList<>();
        for (String[] strings : list) {
            UserBuilder userBuilder=new UserBuilder(strings);
            User user = userBuilder.buildUser().build();
            userList.add(user);
        }
        saveBatch(userList);
        return Result.success("上传成功");
    }
}